package com.clip.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.action.util.SessionAttributeKey;
import com.clip.entity.ClipEvent;
import com.clip.entity.ClipeventPic;
import com.clip.entity.Contact;
import com.clip.entity.ShareEvent;
import com.clip.entity.Sharemapping;
import com.clip.entity.User;
import com.clip.entity.virtual.EventForView;
import com.clip.exception.ClipEventNotExistException;
import com.clip.service.ContactService;
import com.clip.service.EventService;
import com.clip.service.SystemDateService;
import com.clip.service.UserService;
import com.clip.util.Lunar;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.clip.util.URLReplace;

@Controller
@Scope("prototype")
public class ShareEventAction {
	@Autowired
	private EventService eventService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private UserService userService;
	@Autowired
	private SystemDateService systemDateService;
	
	private SimpleDateFormat dateFormat=new SimpleDateFormat("MMdd");
	
	public void saveShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		ClipEvent shareevent;
		try {
			shareevent = this.eventService.saveShareEvent(eventid,currentUser);
			Struts2Utils.renderText("{result:1,shareid:'"+shareevent.getShareid()+"',eventid:"+shareevent.getId()+"}");
		} catch (ClipEventNotExistException e) {
			Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
		}
	}
	
	public void interestShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		boolean isconfirmevent=this.eventService.checkIsInvitedEvent(eventid);
		if(isconfirmevent)
			Struts2Utils.renderText("{result:0,refreshflag:1,msg:'档期已经进入一拍即合状态'}");
		else{
			ClipEvent shareevent;
			try {
				shareevent = this.eventService.interestShareEvent(eventid,currentUser);
				Struts2Utils.renderText("{result:1}");
			} catch (ClipEventNotExistException e) {
				Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
			}
		}
	}
	
	public void uninterestShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		boolean isconfirmevent=this.eventService.checkIsInvitedEvent(eventid);
		if(isconfirmevent){
			Struts2Utils.renderText("{result:0,refreshflag:1,msg:'档期已经进入一拍即合状态'}");
		}else{
			try {
				this.eventService.cancelInterest(eventid,currentUser);
				Struts2Utils.renderText("{result:1}");
			} catch (ClipEventNotExistException e) {
				Struts2Utils.renderText("{result:0,refreshflag:0,msg:'"+e.getErrorMessage()+"'}");
			}
		}
	}
	
	public void quitClip(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		try {
			this.eventService.quitClip(eventid,currentUser);
			Struts2Utils.renderText("{result:1}");
		} catch (ClipEventNotExistException e) {
			Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
		}
	}
	
	public void favorShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		try {
			this.eventService.doFavorOnEvent(currentUser,eventid,1);
			Struts2Utils.renderText("{result:1}");
		} catch (ClipEventNotExistException e) {
			Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
		}
	}
	
	public void unfavorShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		try {
			this.eventService.doFavorOnEvent(currentUser,eventid,0);
			Struts2Utils.renderText("{result:1}");
		} catch (ClipEventNotExistException e) {
			Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
		}
	}
	
	public void cancelShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		try {
			shareid = this.eventService.cancelShareEvent(shareid,currentUser);
			Struts2Utils.renderHtml("{result:1}");
		} catch (ClipEventNotExistException e) {
			Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
		}
	}
	
	public void forwardShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		try {
			ClipEvent clipEvent=eventService.forwardShareEvent(eventid,currentUser,share2userlist,mlist,invitetime);
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("result",1);
			jsonObject.put("shareid", clipEvent.getShareid());
			jsonObject.put("title", clipEvent.getTitle());
			jsonObject.put("starttime", clipEvent.getBegintime());
			jsonObject.put("eventid", clipEvent.getId());
			ClipeventPic clipeventPic=this.eventService.getClipeventPic(clipEvent.getId());
			if(clipeventPic!=null){
				jsonObject.put("clipeventpic", clipeventPic.getPicurl());
			}else{
				jsonObject.put("clipeventpic", "");
			}
			Struts2Utils.renderJson(jsonObject.toString());
		} catch (ClipEventNotExistException e) {
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("result",0);
			jsonObject.put("msg", e.getErrorMessage());
			Struts2Utils.renderJson(jsonObject.toString());
		}
	}
	
	public void forwardMoreWithShareEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		try {
			eventService.forwardMoreWithShareEvent(eventid,currentUser,share2userlist,mlist);
			Struts2Utils.renderText("{result:1}");
		} catch (ClipEventNotExistException e) {
			Struts2Utils.renderText("{result:0,msg:'"+e.getErrorMessage()+"'}");
		}
	}
	
	public void countLastShareEventsByDay(){
		HttpSession session=Struts2Utils.getSession();
		Long getLastCreatetime=(Long) session.getAttribute(SessionAttributeKey.LASTGETSHAREEVENTSTIME);
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long size=eventService.countLastShareEventsByDay(this.year,this.month,this.date,this.contacttype,currentUser,getLastCreatetime);
		Struts2Utils.renderText("{result:1,count:"+size+"}");
	}
	
	/**
	 *  分享广场或好友分享，每日的分享事件
	 */
	public void getShareEventsByDay(){
		Calendar calendar=Calendar.getInstance();
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long lastquerycreattime=0l;
		int lastcurrentpage=-1;
		if(needmore==1){
			lastquerycreattime=(Long) session.getAttribute(this.contacttype+"_"+SessionAttributeKey.LASTQUERYCREATETIME);
			lastcurrentpage=(Integer) session.getAttribute(this.contacttype+"_"+SessionAttributeKey.LASTQUERYPAGENUM);
		}else{
			session.setAttribute(this.contacttype+"_"+SessionAttributeKey.LASTGETSHAREEVENTSTIME,calendar.getTimeInMillis());
		}
		List events1=eventService.getShareEventsByDay(this.year,this.month,this.date,this.contacttype,currentUser,lastquerycreattime,lastcurrentpage);
		if(events1!=null&&events1.size()>0){
			ShareEvent shareEvent=(ShareEvent) events1.get(0);
			if(lastquerycreattime==0l)
				session.setAttribute(this.contacttype+"_"+SessionAttributeKey.LASTQUERYCREATETIME,shareEvent.getCreatetime());
			session.setAttribute(this.contacttype+"_"+SessionAttributeKey.LASTQUERYPAGENUM,lastcurrentpage+1);
		}
		Map returnmap=organizeShareViewMapAsXml(events1,currentUser);
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");
		returnmap.put("firstday", dateFormat.format(firstDate.getTime()));
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnmap));
	}
	
	public void setClipPlazaDetailQuery(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long lastquerycreattime=0l;
		Object temp=session.getAttribute(this.contacttype+"_"+SessionAttributeKey.LASTQUERYCREATETIME);
		if(temp!=null)
			lastquerycreattime=(Long)temp;
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		String query=this.eventService.getQueryContentAtClipPlaza(this.contacttype,currentUser,lastquerycreattime,firstDate);
		session.setAttribute(SessionAttributeKey.DETAILCLIPQUERYCONTENT, query);
		Struts2Utils.renderHtml("{result:1}");
	}
	
	public void setUserDetailQuery(){
		Calendar calendar=Calendar.getInstance();
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long lastquerycreatetime=0l;
		Object temp=session.getAttribute(this.visituid+"_"+SessionAttributeKey.LASTQUERYCREATETIME);
		if(temp!=null)
			lastquerycreatetime=(Long) temp;
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		String query=this.eventService.getQueryContentAtClipUser(this.visituid,currentUser.getId(),lastquerycreatetime,firstDate);
		session.setAttribute(SessionAttributeKey.DETAILCLIPQUERYCONTENT, query);
		Struts2Utils.renderHtml("{result:1}");
	}
	
	private Map organizeShareViewMapAsXml(List events,User currentUser) {
		Map returnMap=new LinkedHashMap();
		if(events==null||events.size()==0)
			return returnMap;
//		Set sets=map.keySet();
		Calendar now=Calendar.getInstance();
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		for (Object object : events) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			ShareEvent shareevent=(ShareEvent) object;
			boolean isself=shareevent.getUid()==currentUser.getId();
			int reliedclip=shareevent.getReliedclip();
			int uid=shareevent.getUid();
			if(reliedclip>0){
				ClipEvent parentEvent=this.eventService.getClipEventById(reliedclip);
				if(parentEvent.getUid()==currentUser.getId()){
					temp2.put("isself", true);
					temp2.put("unameid",uid);
					temp2.put("uname", this.contactService.getUserName(currentUser.getId(),uid));
					List urllist=this.userService.getUserLogoByUid(uid);
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					temp2.put("pname", "我");
					temp2.put("pnameid", currentUser.getId());
					urllist=this.userService.getUserLogoByUid(currentUser.getId());
					temp2.put("pnamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				}else{
					temp2.put("isself", false);
					String contactname=this.contactService.getUserName(currentUser.getId(),uid);
					if(parentEvent.getUid()==uid){
						temp2.put("unameid",uid);
						temp2.put("uname", contactname);
						List urllist=this.userService.getUserLogoByUid(uid);
						temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
						temp2.put("pname", contactname);
						temp2.put("pnameid",uid);
						temp2.put("pnamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					}else{
						temp2.put("unameid",uid);
						temp2.put("uname", contactname);
						List urllist=this.userService.getUserLogoByUid(uid);
						temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
						temp2.put("pnameid",parentEvent.getUid());
						temp2.put("pname", this.contactService.getUserName(currentUser.getId(),parentEvent.getUid()));
						urllist=this.userService.getUserLogoByUid(parentEvent.getUid());
						temp2.put("pnamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					}
				}
				if(shareevent.getEventtype()==2){
					temp2.put("clipusertype", 2);
				}else{
					temp2.put("clipusertype", 1);
				}
				if(parentEvent.getEventtype()==2){
					temp2.put("reliedclipusertype", 2);
				}else{
					temp2.put("reliedclipusertype", 1);
				}
			}else{
				if(uid==currentUser.getId()){
					temp2.put("isself", true);
					temp2.put("uname", "我");
					temp2.put("unameid", currentUser.getId());
					List urllist=this.userService.getUserLogoByUid(currentUser.getId());
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					temp2.put("pname", "");
				}else{
					temp2.put("isself", false);
					String contactname=this.contactService.getUserName(currentUser.getId(),uid);
					temp2.put("unameid",uid);
					temp2.put("uname", contactname);
					List urllist=this.userService.getUserLogoByUid(uid);
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					temp2.put("pname", "");
				}
				if(shareevent.getEventtype()==2){
					temp2.put("clipusertype", 2);
				}else{
					temp2.put("clipusertype", 1);
				}
			}
			
			temp2.put("shareid", shareevent.getShareid());
			temp2.put("starttime", shareevent.getBegintime().toString());
			temp2.put("endtime", shareevent.getEndtime().toString());
			temp2.put("iswholeday", shareevent.getIswholeday().toString());
			String content=shareevent.getContent();
			content=content==null||content.length()==0?"":content;
			temp2.put("content", ToolsUtils.xmlCDATA(URLReplace.replace(content)));
			temp2.put("address", shareevent.getAddress());
			temp2.put("title", ToolsUtils.xmlCDATA(shareevent.getTitle()));
			temp2.put("eventtype", ClipEvent.eventtypeInt2Str(shareevent.getEventtype()));
			int timesituation=checkShareEventTimeSituation(shareevent);
			temp2.put("timesituation", timesituation);
			temp2.put("reliedclip", shareevent.getReliedclip().toString());
//			eventService.updateProcessstatus(shareevent,now);
//			temp2.put("processstatus", shareevent.getProcessstatus().toString());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			temp2.put("isuserself", shareevent.getUid()==currentUser.getId());
			Integer eventid=shareevent.getEventid();
			ClipEvent acceptShareEvent=this.eventService.getUserAcceptShareEvent(eventid, currentUser.getId());
			if(acceptShareEvent!=null){
				temp2.put("isaccept", true);
				temp2.put("acceptshareid",acceptShareEvent.getShareid()!=null&&acceptShareEvent.getShareid().length()>0?acceptShareEvent.getShareid():"");
			}else
				temp2.put("isaccept", false);
			returnMap.put("ShareEvent id="+shareevent.getEventid(), temp2);
		}
		return returnMap;
	}

	/**
	 * @param shareEvent
	 * @return 	0 	进行中
	 * 		 	1	今天结束
	 * 			-1	今天开始
	 */
	private int checkShareEventTimeSituation(ShareEvent shareEvent){
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		if(shareEvent.getBegintime()>=firstDate.getTimeInMillis()&&shareEvent.getBegintime()<secondDate.getTimeInMillis()){
			return -1;
		}else if(shareEvent.getEndtime()>=firstDate.getTimeInMillis()&&shareEvent.getEndtime()<secondDate.getTimeInMillis()&&shareEvent.getIswholeday()==0){
			return 1;
		}else{
			return 0;
		}
	}
	
	public void getviewShareEventsByDay(){
		Calendar now=Calendar.getInstance();
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		List events1=eventService.getviewShareEventsByDay(now,this.contacttype,currentUser);
		Map returnmap=organizeWeekViewMapAsXml(events1,currentUser);
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnmap));
	}
	
	private Map organizeWeekViewMapAsXml(List events,User user) {
		Map returnMap=new LinkedHashMap();
		Calendar now=Calendar.getInstance();
		for (Object object : events) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			ShareEvent shareevent=(ShareEvent) object;
			int uid=shareevent.getUid();
			temp2.put("isuserself", uid==user.getId());
			if(uid==user.getId()){
				temp2.put("uname", "我");
			}else{
				String contactname=this.contactService.getUserName(user.getId(),uid);
				temp2.put("uname", contactname);
			}
			List list=this.userService.getUserLogoByUid(uid);
			temp2.put("uid", uid);
			temp2.put("userlogo", list.get(1));
			temp2.put("starttime", shareevent.getBegintime());
			temp2.put("title", ToolsUtils.xmlCDATA(shareevent.getTitle()));
			temp2.put("eventtype", ClipEvent.eventtypeInt2Str(shareevent.getEventtype()));
			temp2.put("shareid", shareevent.getShareid());
			Integer eventid=shareevent.getEventid();
			returnMap.put("ShareEvent id="+shareevent.getEventid(), temp2);
		}
		return returnMap;
	}
	
	
	private Integer getEndDayTime(ShareEvent shareevent, Calendar secondDate,int startindex) {
		if(shareevent.getIswholeday()==1){
			if(startindex%2==0){
				return startindex;
			}else{
				return startindex+1;
			}
		}else{
			long endtime=shareevent.getEndtime();
			if(endtime>=secondDate.getTimeInMillis()){
				return 2;
			}else{
				Calendar temp1=(Calendar) secondDate.clone();
				Calendar temp2=(Calendar) secondDate.clone();
				temp1.add(Calendar.HOUR, -12);
				
				if(endtime<temp2.getTimeInMillis()&&endtime>=temp1.getTimeInMillis()){
					return 2;
				}else{
					return 1;
				}
			}
		}
	}

	private Integer getStartTime(Long begintime, Calendar firstDate) {
		if(begintime<firstDate.getTimeInMillis()){
			return 1;
		}else{
			Calendar temp1=(Calendar) firstDate.clone();
			Calendar temp2=(Calendar) firstDate.clone();
			temp2.add(Calendar.HOUR, 12);
			if(begintime<temp2.getTimeInMillis()&&begintime>=temp1.getTimeInMillis()){
				return 1;
			}else{
				return 2;
			}
		}
	}

	/**
	 *  user.html 加载右边日历请求
	 */
	public void loadUsersShareByMonth(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		int uid=0;
		try {
			uid=Integer.parseInt(this.visituid);
		} catch (NumberFormatException e) {
			uid=0;
		}
		User visitUser=null;
		if(uid==0){
			visitUser=currentUser;
		}else
			visitUser=this.userService.getUserById(uid);
		if(visitUser==null||visitUser.getStatus()!=1){
			Struts2Utils.renderXml(ToolsUtils.getDOM(null));
			return;
		}
		Calendar calendar=Calendar.getInstance();
		
		if(this.year==0&&this.month==0){
			this.year=calendar.get(Calendar.YEAR);
			this.month=calendar.get(Calendar.MONTH);
		}
		Map returnmap=organizeUsersShareMonthViewMapAsXml(visitUser,currentUser);
		returnmap.put("tdyear", calendar.get(Calendar.YEAR));
		returnmap.put("tdmonth", calendar.get(Calendar.MONTH));
		returnmap.put("tddate", calendar.get(calendar.DAY_OF_MONTH));
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnmap));
		
	}
	
	
	private Map organizeUsersShareMonthViewMapAsXml(User visitUser,
			User currentUser) {
		Map returnMap=new LinkedHashMap();
		Calendar now=Calendar.getInstance();
		Calendar firstDateOfMonth=Calendar.getInstance();
		firstDateOfMonth.clear(Calendar.MILLISECOND);
		firstDateOfMonth.clear(Calendar.SECOND);
		firstDateOfMonth.clear(Calendar.MINUTE);
		firstDateOfMonth.set(Calendar.HOUR_OF_DAY, 0);
		firstDateOfMonth.set(Calendar.YEAR, year);
		firstDateOfMonth.set(Calendar.MONTH,month);
		firstDateOfMonth.set(Calendar.DAY_OF_MONTH, 1);
		
		Calendar startdate=(Calendar) firstDateOfMonth.clone();
		startdate.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY-startdate.get(Calendar.DAY_OF_WEEK));
		
		Calendar lastDateOfMonth=(Calendar) firstDateOfMonth.clone();
		lastDateOfMonth.set(Calendar.DAY_OF_MONTH,firstDateOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
		
		Calendar lastDate=(Calendar) lastDateOfMonth.clone();
		lastDate.add(Calendar.DAY_OF_MONTH, Calendar.SATURDAY-lastDate.get(Calendar.DAY_OF_WEEK));
		returnMap.put("year_month value="+this.year+"_"+this.month,new HashMap());
		int holiday=0;
		while(!startdate.after(lastDate)){
			//查看该天是否有分享
			Long size=this.eventService.countShareEventsByDayAndUser(startdate, visitUser,currentUser);
//			Long endsize=this.eventService.countEndedShareEventsByDayAndUser(startdate, visitUser,currentUser.getId());
			int isholiday=isSysHoliday(startdate.get(Calendar.YEAR), startdate.get(Calendar.MONTH), startdate.get(Calendar.DAY_OF_MONTH));
			int weeknum=startdate.get(Calendar.DAY_OF_WEEK);
			boolean isweekend=weeknum==1||weeknum==7?true:false;
			int isThisMonth=startdate.get(Calendar.MONTH)==this.month?1:0;
			
				
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("isThisMonth", ""+isThisMonth);
			temp2.put("size", size);
//			temp2.put("endsize", endsize);
			//temp2.put("alerteventsize", alertcount);
			temp2.put("weeknum", ""+startdate.get(Calendar.WEEK_OF_YEAR));
			temp2.put("weekofmonth", startdate.get(Calendar.WEEK_OF_MONTH));
			temp2.put("weekday", startdate.get(Calendar.DAY_OF_WEEK));
			temp2.put("lunartxt", Lunar.getLunarText(startdate));
			if(isweekend){
				if(isholiday==1||isholiday==-1){ //周末也是节日
					holiday=1;
					returnMap.put("month"+dateFormat.format(startdate.getTime())+" holiday="+holiday,temp2);
				}else{ //周末不休息
					holiday=0;
					returnMap.put("month"+dateFormat.format(startdate.getTime())+" holiday="+holiday,temp2);
				}
			}else{
				if(isholiday==1){ //平时但是节日
					holiday=1;
					returnMap.put("month"+dateFormat.format(startdate.getTime())+" holiday="+holiday,temp2);
				}else{ //平时也不是节日
					holiday=0;
					returnMap.put("month"+dateFormat.format(startdate.getTime())+" holiday="+holiday,temp2);
				}
			}
			startdate.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		return returnMap;
	}
	private int isSysHoliday(int year, int month, int day) {
		return systemDateService.getSystemHolidayByDay(year, month, day);
	}

	public void getUsersShareEventsByDay(){
		Calendar calendar=Calendar.getInstance();
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long lastquerycreatetime=0l;
		int lastcurrentpage=-1;
		if(needmore==1){
			lastquerycreatetime=(Long) session.getAttribute(this.visituid+"_"+SessionAttributeKey.LASTQUERYCREATETIME);
			lastcurrentpage=(Integer) session.getAttribute(this.visituid+"_"+SessionAttributeKey.LASTQUERYPAGENUM);
		}
		List events1=eventService.getUsersShareEventsByDay(this.year,this.month,this.date,this.visituid,currentUser,lastquerycreatetime,lastcurrentpage);
		if(events1!=null&&events1.size()>0){
			ClipEvent clipEvent=(ClipEvent) events1.get(0);
			if(lastquerycreatetime==0l)
				session.setAttribute(this.visituid+"_"+SessionAttributeKey.LASTQUERYCREATETIME,clipEvent.getCreatetime());
			session.setAttribute(this.visituid+"_"+SessionAttributeKey.LASTQUERYPAGENUM,lastcurrentpage+1);
		}
		Map returnmap=organizeUsersShareViewMapAsXml(events1,currentUser);
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");
		returnmap.put("firstday", dateFormat.format(firstDate.getTime()));
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnmap));
	}
	
	
	private Map organizeUsersShareViewMapAsXml(List events, User currentUser) {
		int vuid=Integer.parseInt(this.visituid);
		Map returnMap=new LinkedHashMap();
		if(events==null||events.size()==0)
			return returnMap;
		Calendar now=Calendar.getInstance();
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		String contactname=null;
		boolean isuserself=false;
		boolean isuser=currentUser!=null&&currentUser.getStatus()==1;
		if(isuser){
			if(currentUser.getId()==vuid||vuid==0){
				isuserself=true;
				contactname="我";
			}else
				contactname=this.contactService.getUserName(currentUser.getId(),vuid);
		}else{
			contactname=this.contactService.getUserName(0,vuid);
		}
		for (Object object : events) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			ClipEvent clipevent=(ClipEvent) object;
			int reliedclip=clipevent.getReliedclip();
			temp2.put("reliedclip", reliedclip);
			temp2.put("uname", contactname);
			temp2.put("unameid", vuid);
			List urllist=this.userService.getUserLogoByUid(vuid);
			temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(0)));
			temp2.put("unameslogo", ToolsUtils.xmlCDATA(urllist.get(1)));
			temp2.put("cliptype",clipevent.getCliptype());
			temp2.put("seid",clipevent.getSeid());
			if(clipevent.getSeid()>0){
				ClipEvent sourceEvent=this.eventService.getDetailClipEvent(clipevent.getSeid());
				User sourceUser=this.userService.getUserById(sourceEvent.getUid());
				temp2.put("sourceeventid", sourceEvent.getId());
				temp2.put("sourceuid", sourceEvent.getUid());
				temp2.put("sourceuname", this.contactService.getUserName(0, sourceUser.getId()));
				List sourceuserlist=this.userService.getUserLogoByUid(sourceUser.getId());
				temp2.put("sourceulogo", sourceuserlist.get(1));
			}
//			if(vuid==0){
//				urllist=this.userService.getUserLogoByUid(currentUser.getId());
//			}else
//				urllist=this.userService.getUserLogoByUid(vuid);
//			temp2.put("userlogo", ToolsUtils.xmlCDATA(urllist.get(0)));
//			temp2.put("userlogosmall", ToolsUtils.xmlCDATA(urllist.get(1)));
			temp2.put("shareid", clipevent.getShareid());
			temp2.put("starttime", clipevent.getBegintime().toString());
			temp2.put("endtime", clipevent.getEndtime().toString());
			temp2.put("invitetime", clipevent.getInvitetime().toString());
			temp2.put("iswholeday", clipevent.getIswholeday().toString());
			temp2.put("content", ToolsUtils.xmlCDATA(clipevent.getContent()));
			temp2.put("title", ToolsUtils.xmlCDATA(clipevent.getTitle()));
			temp2.put("eventtype",clipevent.getEventtype());
			temp2.put("vcode", clipevent.getVcode());
			
			
			temp2.put("deadlinetime", clipevent.getDeadlinetime());
			temp2.put("iscanfw", clipevent.getIscanfw());
			String address=clipevent.getAddress();
			if(address==null||address.length()<=0){
				temp2.put("address", "");
			}else{
				if(address.indexOf(":")>0){
					String []addarr=address.split(":");
					String preaddress=addarr[0];
					String sufaddress=addarr[1];
					if(preaddress.indexOf("_")>0){
						String[]areaarr=preaddress.split("_");
						int pid=Integer.parseInt(areaarr[0]);
						String padd=UserService.getRealAddress("Province",pid);
						int cid=Integer.parseInt(areaarr[1]);
						String cadd=null;
						String dadd=null;
						if(cid==0){
							cadd="";
						}else{
							cadd=UserService.getRealAddress("City",cid);
						}
						if(areaarr.length==3){
							int did=Integer.parseInt(areaarr[2]);
							if(did==0){
								dadd="";
							}else{
								dadd=UserService.getRealAddress("District", did);
							}
						}else{
							dadd="";
						}
						temp2.put("address", padd+cadd+dadd+" "+sufaddress);
					}else{
						int pid=Integer.parseInt(preaddress);
						String padd=UserService.getRealAddress("Province",pid);
						if(padd==null){
							temp2.put("address", sufaddress);
						}else
							temp2.put("address", padd+" "+sufaddress);
						
					}
					
				}else{
					temp2.put("address", address);
				}
			}
			
			
			ClipeventPic clipeventPic=this.eventService.getClipeventPic(clipevent.getId());
			if(clipeventPic!=null){
				temp2.put("clipeventpic", clipeventPic.getPicurl());
			}else{
				temp2.put("clipeventpic", "");
			}
			if(isuser)
				temp2.put("isuserself", clipevent.getUid()==currentUser.getId());
			else
				temp2.put("isuserself", false);
//			eventService.updateOverDeadlinetime(clipevent,now);
//			temp2.put("processstatus", clipevent.getProcessstatus().toString());
			Integer eventid=clipevent.getId();
			if(clipevent.getCliptype()==3&&isuser){
				if(isuserself){
					temp2.put("clocktime", clipevent.getAlerttime());
				}else{
						
					int result=this.eventService.getInviteEventInterestORNOT(eventid, currentUser.getId());
					if(result>0){
						ClipEvent interestedevent=this.eventService.getDetailClipEvent(result);
						temp2.put("clocktime", interestedevent.getAlerttime());
						temp2.put("eventisinterested", 1);
					}else{
						temp2.put("eventisinterested", 0);
					}
					boolean isshielded=this.eventService.checkIsShielded(clipevent.getId(), currentUser.getId());
					temp2.put("isshielded", isshielded);
				}
//				boolean isforward=this.eventService.checkClipEventForwardedORNOT(clipevent.getId(), currentUser.getId());
//				temp2.put("isforward", isforward);
				boolean isconfirmevent=false;
				isconfirmevent=this.eventService.checkIsInvitedEvent(clipevent.getId());
				temp2.put("isconfirmevent", isconfirmevent);
			}else{
				if(isuserself)
					temp2.put("clocktime", clipevent.getAlerttime().toString());
			}
			returnMap.put("ClipEvent id="+clipevent.getId(), temp2);
		}
		return returnMap;
	}

	public void loadShareEvent(){
		HttpServletRequest request=Struts2Utils.getRequest();
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		ClipEvent shareEvent=this.eventService.getDetailShareEvent(shareid);
		if(shareEvent==null){
			Struts2Utils.renderHtml("");
			return;
		}
		int reliedclip=shareEvent.getReliedclip();
//		Map contactMap=this.eventService.getClipInvitedPerson(id,currentUser);
		
		Map<String, Object> returnMap=new LinkedHashMap<String, Object>();
		Map<String,Object> temp2=new HashMap<String,Object>();
		temp2.put("eventid", shareEvent.getId());
		temp2.put("title", ToolsUtils.xmlCDATA(shareEvent.getTitle()));
		String contactname=null;
		if(currentUser==null){
			temp2.put("isself", false);
			contactname=this.contactService.getUserName(0,shareEvent.getUid());
			List urllist=this.userService.getUserLogoByUid(shareEvent.getUid());
			if(reliedclip>0){
				ClipEvent clipEvent=this.eventService.getClipEventById(reliedclip);
				String tempcontactname=this.contactService.getUserName(0,clipEvent.getUid());
				temp2.put("uname", contactname);
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				urllist=this.userService.getUserLogoByUid(clipEvent.getUid());
				temp2.put("pname", tempcontactname);
				temp2.put("pnamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
			}else{
				temp2.put("uname", contactname);
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				temp2.put("pname", "");
			}
		}else{
			boolean isself=false;
			boolean isaccept=false;
			if(currentUser.getId()==shareEvent.getUid()){
				isself=true;
				temp2.put("isself", true);
				contactname="我";
			}else{
				temp2.put("isself", false);
				contactname=this.contactService.getUserName(currentUser.getId(),shareEvent.getUid());
				ClipEvent acceptevent=this.eventService.getUserAcceptShareEvent(shareEvent.getId(),currentUser.getId());
				if(acceptevent!=null){
					temp2.put("isaccept", true);
					temp2.put("acceptshareid", acceptevent.getShareid()!=null&&acceptevent.getShareid().length()>0?acceptevent.getShareid():"");
				}else{
					temp2.put("isaccept", false);
				}
			}
			
			if(isself||isaccept){
				int followeventid=this.eventService.getUserAcceptShareEventid(shareEvent,currentUser.getId());
				if(followeventid>0){
					List sharemapslist=this.eventService.getShareMappingList(followeventid);
					for (int i=0;i<sharemapslist.size();i++) {
						Sharemapping sharemapping=(Sharemapping) sharemapslist.get(i);
						int share2uid=sharemapping.getShare2uid();
						if(share2uid==0){
							List logolist=this.userService.getUserLogoByUid(0);
							temp2.put("sharemembers index=-1 name=全部用户",ToolsUtils.xmlCDATA(logolist.get(1)));
						}else{
							String name=this.contactService.getUserName(currentUser.getId(), share2uid);
							List logolist=this.userService.getUserLogoByUid(share2uid);
							temp2.put("sharemembers index="+i+" uid="+share2uid+" name="+name,ToolsUtils.xmlCDATA(logolist.get(1)));
						}
					}
				}
			}
			
			List urllist=this.userService.getUserLogoByUid(shareEvent.getUid());
			if(reliedclip>0){
				ClipEvent clipEvent=this.eventService.getClipEventById(reliedclip);
				if(currentUser!=null&&clipEvent.getUid()==currentUser.getId()){
					temp2.put("unameid",shareEvent.getUid());
					temp2.put("uname", contactname);
					temp2.put("pnameid",currentUser.getId());
					temp2.put("pname", "我");
				}else{
					String tempcontactname=this.contactService.getUserName(currentUser.getId(),clipEvent.getUid());
					temp2.put("uname", contactname);
					temp2.put("unameid",shareEvent.getUid());
					temp2.put("pname", tempcontactname);
					temp2.put("pnameid",clipEvent.getUid());
				}
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				urllist=this.userService.getUserLogoByUid(clipEvent.getUid());
				temp2.put("pnamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
			}else{
				temp2.put("unameid",shareEvent.getUid());
				temp2.put("uname", contactname);
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				temp2.put("pname", "");
			}
		}
		temp2.put("eventtype",ClipEvent.eventtypeInt2Str(shareEvent.getEventtype()));
		temp2.put("starttime",shareEvent.getBegintime());
		temp2.put("endtime", shareEvent.getEndtime());
		temp2.put("iswholeday", shareEvent.getIswholeday());
		String content=shareEvent.getContent();
		content=content==null||content.length()==0?"":ToolsUtils.xmlCDATA(URLReplace.replace(content));
		temp2.put("content", content);
		temp2.put("clocktime", shareEvent.getAlerttime());
		temp2.put("address", shareEvent.getAddress()==null||shareEvent.getAddress().length()==0?"":ToolsUtils.xmlCDATA(shareEvent.getAddress()));
//		temp2.put("deadlinetime", shareEvent.getDeadlinetime());
		temp2.put("reliedclip",shareEvent.getReliedclip());
//		temp2.put("processstatus",shareEvent.getProcessstatus());
		List list=this.eventService.getStatAboutClipEvent(shareEvent,currentUser); //得到分享档期的统计
		temp2.put("countsharesave", list.get(0));
		temp2.put("countshareforward", list.get(1));
		if(currentUser!=null)
			temp2.put("shareid",shareEvent.getShareid());
//		for(int i=0;i<contactMap.keySet().size();i++){
//			Map<String,Object> temp1=new HashMap<String,Object>();
//			List list=(List) contactMap.get(i+1);
//			temp1.put("name", list.get(1));
//			temp1.put("email", list.get(2));
//			temp1.put("status", list.get(3));
//			temp1.put("realname", list.get(4));
//			temp2.put("contact uid="+list.get(0),temp1);
//		}
		returnMap.put("ShareEvent id="+shareEvent.getId(), temp2);
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnMap));
	}

	public void loadsfFriends(){
		HttpServletRequest request=Struts2Utils.getRequest();
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Map map=new HashMap();
		if(currentUser!=null){
			List users=this.contactService.findShareFriendsByEventid(eventid,currentUser);
			for (Object object : users) {
				Map temp=new HashMap();
				Integer uid=(Integer) object;
				User user=this.userService.getUserById(uid);
				temp.put("nickname",this.contactService.getUserName(currentUser.getId(), uid));
				temp.put("realname",user.getName());
				map.put("User uid="+uid, temp);
			}
			Struts2Utils.renderXml(ToolsUtils.getDOM(map));
		}else{
			Struts2Utils.renderHtml("");
		}
	}
	
	public void noneuserFavorShareEvent(){
		this.eventService.noneuserFavorShareEvent(eventid);
		JSONObject returnJson=new JSONObject();
		returnJson.put("result", 1);
		Struts2Utils.renderJson(returnJson.toString());
	}
	
	private int year;
	private int month;
	private int date;
	private int contacttype; //0为全部 1为好友
	private int needmore;//0：加载最新的,1：session中读取记录的createtime
	private int eventid;
	private int isview;
	private String sharelist;
	private String share2userlist;
	private String mlist;
	private String visituid;
	private String shareid;
	private long invitetime=0l;
	public long getInvitetime() {
		return invitetime;
	}
	public void setInvitetime(long invitetime) {
		this.invitetime = invitetime;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDate() {
		return date;
	}
	public void setDate(int date) {
		this.date = date;
	}
	public int getContacttype() {
		return contacttype;
	}

	public void setContacttype(int contacttype) {
		this.contacttype = contacttype;
	}

	public int getNeedmore() {
		return needmore;
	}

	public void setNeedmore(int needmore) {
		this.needmore = needmore;
	}

	public int getEventid() {
		return eventid;
	}

	public void setEventid(int eventid) {
		this.eventid = eventid;
	}

	public String getSharelist() {
		return sharelist;
	}

	public void setSharelist(String sharelist) {
		this.sharelist = sharelist;
	}

	public int getIsview() {
		return isview;
	}

	public void setIsview(int isview) {
		this.isview = isview;
	}

	public String getVisituid() {
		return visituid;
	}

	public void setVisituid(String visituid) {
		this.visituid = visituid;
	}

	public String getShareid() {
		return shareid;
	}

	public void setShareid(String shareid) {
		this.shareid = shareid;
	}

	public String getShare2userlist() {
		return share2userlist;
	}

	public void setShare2userlist(String share2userlist) {
		this.share2userlist = share2userlist;
	}

	public String getMlist() {
		return mlist;
	}

	public void setMlist(String mlist) {
		this.mlist = mlist;
	}
	
}
